<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Utilities.Functions"%>
<%@page import="Utilities.ApplicationException"%>
<%@page import="Managers.Utilities.RequestResponseWrapper"%>
<%@page import="Managers.Utilities.ResponseMessagesContainer"%>
<%@page import="Utilities.Constants"%>
<%@page import="Beans.Requests.NetworkManagement.RemoveInternalNetworkRequest"%>
<%@page import="Beans.Requests.NetworkManagement.RemoveHostOnlyInterfaceRequest"%>
<%@page import="Beans.Requests.NetworkManagement.RemoveDHCPServerRequest"%>
<%@page import="Beans.Requests.NetworkManagement.ModifyInternalNetworkRequest"%>
<%@page import="Beans.Requests.NetworkManagement.ModifyHostInterfaceRequest"%>
<%@page import="Beans.Requests.NetworkManagement.ModifyDHCPServerRequest"%>
<%@page import="Beans.Requests.NetworkManagement.Components.DHCPServerConfiguration"%>
<%@page import="Utilities.Constants.NetworkType"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Beans.Requests.NetworkManagement.CreateInternalNetworkRequest"%>
<%@page import="Beans.Requests.NetworkManagement.CreateHostOnlyInterfaceRequest"%>
<%@page import="Beans.Requests.NetworkManagement.CreateDHCPServerRequest"%>
<%@page import="Utilities.UIFunctions"%>
<%@page import="Managers.Utilities.User"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Beans.Responses.NetworkManagementResponse"%>
<%@page import="Beans.Requests.NetworkManagement.NetworkManagementRequest"%>
<%
//retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);
    int networkTypeInt = 0;

    //attempts to retrieve the requested action
    int action = 0;
    try
    {
        action = Integer.parseInt(request.getParameter("action"));
    }
    catch(NumberFormatException e){}

    //builds the requested action, based on the action id
    NetworkManagementRequest networkRequest = null;
    switch(action)
    {
        //create DHCP request
        case 1:
        {
            //retrieves supplied the network type
            NetworkType networkType = null;
            try
            {
                int parameterNetworkType = Integer.parseInt(request.getParameter("type"));
                networkTypeInt = parameterNetworkType;
                switch(parameterNetworkType)
                {
                    case 1: networkType = NetworkType.INTERNAL; break;
                    case 2: networkType = NetworkType.HOST_ONLY; break;
                    default: ;
                }
            }
            catch(NumberFormatException e)
            {}

            if(request.getParameter("noconfig") == null)
            {
                DHCPServerConfiguration dhcpConfig = 
                        new DHCPServerConfiguration(request.getParameter("create.dhcp.ipAddress"),
                                                    request.getParameter("create.dhcp.networkMask"),
                                                    request.getParameter("create.dhcp.addressLow"),
                                                    request.getParameter("create.dhcp.addressHigh"),
                                                    false);

                networkRequest = new CreateDHCPServerRequest(userSessionData.getUserData().getUserId(),
                                                             userSessionData.getCurrentServerId(),
                                                             request.getParameter("id"),
                                                             networkType,
                                                             dhcpConfig,
                                                             (request.getParameter("create.dhcp.enable") != null));
            }
            else
            {
                networkRequest = new CreateDHCPServerRequest(userSessionData.getUserData().getUserId(),
                                                             userSessionData.getCurrentServerId(),
                                                             request.getParameter("create.dhcp.networkId"),
                                                             networkType);
            }
        } break;

        //create Host-Only Interface request
        case 2:
        {
            networkTypeInt = 2;
            networkRequest = new CreateHostOnlyInterfaceRequest(userSessionData.getUserData().getUserId(),
                                                                userSessionData.getCurrentServerId(),
                                                                request.getParameter("create.hostOnly.permissions"));
        } break;

        //create Internal network request
        case 3:
        {
            networkTypeInt = 1;
            networkRequest = new CreateInternalNetworkRequest(userSessionData.getUserData().getUserId(),
                                                              userSessionData.getCurrentServerId(),
                                                              request.getParameter("create.internal.name"),
                                                              request.getParameter("create.internal.permissions"));
        } break;

        //modify DHCP request
        case 4:
        {
            //retrieves the supplied network type
            NetworkType networkType = null;
            try
            {
                int parameterNetworkType = Integer.parseInt(request.getParameter("type"));
                networkTypeInt = parameterNetworkType;
                switch(parameterNetworkType)
                {
                    case 1: networkType = NetworkType.INTERNAL; break;
                    case 2: networkType = NetworkType.HOST_ONLY; break;
                    default: ;
                }
            }
            catch(NumberFormatException e)
            {}

            networkRequest = new ModifyDHCPServerRequest(userSessionData.getUserData().getUserId(),
                                                         userSessionData.getCurrentServerId(),
                                                         request.getParameter("id"),
                                                         networkType);


            if(request.getParameter("modify.dhcp.enable.submit") != null)
                ((ModifyDHCPServerRequest)networkRequest).setDHCPState(true);
            else if(request.getParameter("modify.dhcp.disable.submit") != null)
                ((ModifyDHCPServerRequest)networkRequest).setDHCPState(false);
            else
                ;

            if(request.getParameter("modify.dhcp.config.submit") != null)
            {
                DHCPServerConfiguration dhcpConfig =
                        new DHCPServerConfiguration(request.getParameter("modify.dhcp.ipAddress"),
                                                    request.getParameter("modify.dhcp.networkMask"),
                                                    request.getParameter("modify.dhcp.addressLow"),
                                                    request.getParameter("modify.dhcp.addressHigh"),
                                                    false);

                ((ModifyDHCPServerRequest)networkRequest).setServerConfiguration(dhcpConfig);
            }
            else
                ;
        } break;

        //modify Host Interface request
        case 5:
        {
            networkTypeInt = 2;
            networkRequest = new ModifyHostInterfaceRequest(userSessionData.getUserData().getUserId(),
                                                            userSessionData.getCurrentServerId(),
                                                            request.getParameter("id"));
            
            String ipConfig = request.getParameter("modify.hostif.ipConfig");
            if(ipConfig != null)
            {
                if(ipConfig.equalsIgnoreCase("dynamic"))
                    ((ModifyHostInterfaceRequest)networkRequest).enableDynamicIPConfiguration();
                else if(ipConfig.equalsIgnoreCase("staticV6"))
                {
                    long IPv6MaskLength = 0;

                    try
                    {
                        IPv6MaskLength = Integer.parseInt(request.getParameter("modify.hostif.ipv6mask"));
                    }
                    catch(NumberFormatException e){}

                    ((ModifyHostInterfaceRequest)networkRequest).setIPv6StaticConfiguration(request.getParameter("modify.hostif.address"),
                                                                                            IPv6MaskLength);
                }
                else if(ipConfig.equalsIgnoreCase("staticV4"))
                {
                    ((ModifyHostInterfaceRequest)networkRequest).setIPv4StaticConfiguration(request.getParameter("modify.hostif.address"),
                                                                                            request.getParameter("modify.hostif.ipv4mask"));
                }
                else
                    ;
            }
            else
                ;

            if(request.getParameter("modify.hostif.permissions.submit") != null)
                ((ModifyHostInterfaceRequest)networkRequest).setPermissions(request.getParameter("modify.hostif.permissions"));
            else
                ;

            if(request.getParameter("modify.hostif.newOwner.submit") != null)
            {
                int newOwnerId = Constants.INVALID_USER_ID;

                try
                {
                    newOwnerId = Integer.parseInt(request.getParameter("modify.hostif.newOwner"));
                }
                catch(NumberFormatException e){}

                ((ModifyHostInterfaceRequest)networkRequest).setNewOwnerId(newOwnerId);
            }
            else
                ;
        } break;

        //modify Internal network request
        case 6:
        {
            networkTypeInt = 1;
            networkRequest = new ModifyInternalNetworkRequest(userSessionData.getUserData().getUserId(),
                                                              userSessionData.getCurrentServerId(),
                                                              request.getParameter("id"));

            if(request.getParameter("modify.internal.name.submit") != null)
                ((ModifyInternalNetworkRequest)networkRequest).setNewExternalName(request.getParameter("modify.internal.name"));
            else
                ;

            if(request.getParameter("modify.internal.permissions.submit") != null)
                ((ModifyInternalNetworkRequest)networkRequest).setPermissions(request.getParameter("modify.internal.permissions"));
            else
                ;
        } break;

        //remove DHCP request
        case 7:
        {
            //retrieves the supplied network type
            NetworkType networkType = null;
            try
            {
                int parameterNetworkType = Integer.parseInt(request.getParameter("type"));
                networkTypeInt = parameterNetworkType;
                switch(parameterNetworkType)
                {
                    case 1: networkType = NetworkType.INTERNAL; break;
                    case 2: networkType = NetworkType.HOST_ONLY; break;
                    default: ;
                }
            }
            catch(NumberFormatException e)
            {}

            networkRequest = new RemoveDHCPServerRequest(userSessionData.getUserData().getUserId(),
                                                         userSessionData.getCurrentServerId(),
                                                         request.getParameter("id"),
                                                         networkType);
        } break;

        //remove Host-Only Interface request
        case 8:
        {
            networkTypeInt = 2;
            networkRequest = new RemoveHostOnlyInterfaceRequest(userSessionData.getUserData().getUserId(),
                                                                userSessionData.getCurrentServerId(),
                                                                request.getParameter("id"));
        } break;

        //remove Internal network request
        case 9:
        {
            networkTypeInt = 1;
            networkRequest = new RemoveInternalNetworkRequest(userSessionData.getUserData().getUserId(),
                                                              userSessionData.getCurrentServerId(),
                                                              request.getParameter("id"));
        } break;

        default: ;
    }

    //attempts to send the request and retrieves the response
    SendingRequestResult sendingResult = UIFunctions.sendServiceRequest(request, response, networkRequest, Service.NETWORKS_MANAGEMENT);
    if(sendingResult != SendingRequestResult.OK)
    {
        //retrieves the user's messages container
        ResponseMessagesContainer messagesContainer =
            (ResponseMessagesContainer)request.getSession(false).getAttribute(ParameterNames.SESSION_REQUEST_MESSAGES_OBJECT);

        int exceptionCode = UIFunctions.parseSendingRequestResult(sendingResult);

        //adds the response from the failed operation
        NetworkManagementResponse networkResponse = new NetworkManagementResponse(false, new ApplicationException(exceptionCode));
        messagesContainer.put(new RequestResponseWrapper(networkRequest, networkResponse));
    }
    else
        ;

    //based on the requested action, the proper redirect page is selected
    if(Functions.isUUIDValid(request.getParameter("id")) && action < 7)
        response.sendRedirect("networks.jsp?type=" + networkTypeInt + "&id=" + request.getParameter("id"));
    else
        response.sendRedirect("networks.jsp?type=" + networkTypeInt);
%>
